Implement Repo::write_content
authorFelix Krull <f_krull@gmx.de>
Sat, 31 Aug 2019 20:41:42 +0000 (22:41 +0200)
committerColin Walters <walters@verbum.org>
Fri, 6 May 2022 16:53:54 +0000 (12:53 -0400)
rust-bindings/rust/src/repo.rs
rust-bindings/rust/sys/src/manual.rs
rust-bindings/rust/tests/repo/mod.rs

index cc9cc10fb38c7e81175786216816abe0e1ec82a5..8a1a12b839b4dce82161f32b4de83431215ebbb5 100644 (file)
@@ -1,6 +1,6 @@
-use crate::Repo;
 #[cfg(any(feature = "v2016_4", feature = "dox"))]
 use crate::RepoListRefsExtFlags;
+use crate::{Checksum, Repo};
 use gio;
 use glib;
 use glib::translate::*;
@@ -115,4 +115,31 @@ impl Repo {
             }
         }
     }
+
+    pub fn write_content<P: IsA<gio::InputStream>, Q: IsA<gio::Cancellable>>(
+        &self,
+        expected_checksum: Option<&str>,
+        object_input: &P,
+        length: u64,
+        cancellable: Option<&Q>,
+    ) -> Result<Checksum, Error> {
+        unsafe {
+            let mut error = ptr::null_mut();
+            let mut out_csum = ptr::null_mut();
+            let _ = ostree_sys::fixed::ostree_repo_write_content(
+                self.to_glib_none().0,
+                expected_checksum.to_glib_none().0,
+                object_input.as_ref().to_glib_none().0,
+                length,
+                &mut out_csum,
+                cancellable.map(|p| p.as_ref()).to_glib_none().0,
+                &mut error,
+            );
+            if error.is_null() {
+                Ok(from_glib_full(out_csum))
+            } else {
+                Err(from_glib_full(error))
+            }
+        }
+    }
 }
index 4c5af957fbf25cc54f507bc086c65a7d18600b40..4f49eeae0eaacdc078ac7845ea4b0ead1ff1dc4f 100644 (file)
@@ -1 +1,19 @@
-pub use libc::stat as stat;
+pub use libc::stat;
+
+pub mod fixed {
+    use crate::OstreeRepo;
+    use glib::gboolean;
+    use libc::c_char;
+
+    extern "C" {
+        pub fn ostree_repo_write_content(
+            self_: *mut OstreeRepo,
+            expected_checksum: *const c_char,
+            object_input: *mut gio::GInputStream,
+            length: u64,
+            out_csum: *mut *mut [u8; 32],
+            cancellable: *mut gio::GCancellable,
+            error: *mut *mut glib::GError,
+        ) -> gboolean;
+    }
+}
index c0385ba8e60f2782bb065ee647cab27e549ab05a..5c5cba174b48b4e30cdfcada337c186d4cdfd669 100644 (file)
@@ -88,3 +88,30 @@ fn should_checkout_tree() {
 
     assert_test_file(checkout_dir.path());
 }
+
+#[test]
+fn should_write_content_to_repo() {
+    let src = TestRepo::new();
+    let mtree = create_mtree(&src.repo);
+    let checksum = commit(&src.repo, &mtree, "test");
+
+    let dest = TestRepo::new();
+    let objects = src
+        .repo
+        .traverse_commit(&checksum, -1, NONE_CANCELLABLE)
+        .expect("traverse");
+    for obj in objects {
+        let (stream, len) = src
+            .repo
+            .load_object_stream(obj.object_type(), obj.checksum(), NONE_CANCELLABLE)
+            .expect("load object stream");
+        if obj.object_type() == ObjectType::File {
+            let out_csum = dest
+                .repo
+                .write_content(None, &stream, len, NONE_CANCELLABLE)
+                .expect("write content");
+
+            assert_eq!(out_csum.to_string(), obj.checksum());
+        }
+    }
+}